package com.example.proyyj.Controller;


import com.example.proyyj.Entity.Calligraphy;
import com.example.proyyj.Entity.CollectionCalligraphy;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.CollectionCalligraphyService;
import com.example.proyyj.Util.JSONTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/CollectionCalligraphy/")//请求路径
public class CollectionCalligraphyController {
    @Resource
    CollectionCalligraphyService collectionCalligraphyService;
    ResultEntity resultEntity=null;

    @GetMapping("collectionCalligraphy")
    private String collectionCalligraphy(@RequestParam("UserId") int UserId, @RequestParam("CalligraphyId")int CalligraphyId){
        resultEntity = new ResultEntity();

        try{
            CollectionCalligraphy collection=collectionCalligraphyService.collectionCalligraphy(UserId,CalligraphyId);
            if (collection !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("已收藏！");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("未收藏！");
            }

        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PostMapping("addCalligraphy")
    private String addCalligraphy(@RequestParam("UserId") int UserId,@RequestParam("CalligraphyId") int CalligraphyId){
        resultEntity = new ResultEntity();

        try{
            if (collectionCalligraphyService.addCalligraphy(UserId,CalligraphyId)>0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("收藏失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @DeleteMapping("delCalligraphy")
    private String delCalligraphy(@RequestParam("UserId") int UserId,@RequestParam("CalligraphyId") int CalligraphyId){
        resultEntity = new ResultEntity();

        try{
            if (collectionCalligraphyService.delCalligraphy(UserId,CalligraphyId)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("删除失败");
            }


        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }
    @GetMapping("lookCalligraphyList")
    private  String lookCalligraphylist(@RequestParam("UserId") int UserId){
        resultEntity = new ResultEntity();
        try{
            resultEntity = new ResultEntity();
            List<CollectionCalligraphy> list =collectionCalligraphyService.lookCalligraphylist(UserId);
            if ( list !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("查看成功");
                resultEntity.setData(list);
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("查看失败");
            }

        }catch (Exception e){

            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }



    @GetMapping("lookCalligraphyid")
    private String lookCalligraphy(@RequestParam("CalligraphyId") int CalligraphyId){
        resultEntity = new ResultEntity();
        try{
            Calligraphy  collection=collectionCalligraphyService.lookCalligraphy(CalligraphyId);
            if (collection!=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("查询成功");
                resultEntity.setData(collection);
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("未查询成功");
            }

        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }
}
